package com.lemon.funplus;

import java.util.ArrayList;

import android.accounts.AccountManager;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lemon.funplus.object.AccountObject;
import com.lemon.funplus.parse.CMD;
import com.lemon.funplus.parse.JsonParser;
import com.lemon.funplus.ulti.BaseEngine;
import com.lemon.funplus.ulti.PreferenceUtils;
import com.lemon.funplus.ulti.TagName;

public class AccountForgetPassActivity extends BaseActivity {
	EditText edtPhone;
	TextView textCaution;
	String phone="";
	String oldpass="";
	String newpass="";
	String renewpass="";
			
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.account_forgetpass);
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		menuFragment.ACCOUNT.select();
		myActionBar.setTitle(getResources().getString(R.string.forgetpass));
	}

	void initView() {
		edtPhone = (EditText) findViewById(R.id.forgetpass_edittext_phone);
		textCaution = (TextView) findViewById(R.id.forgetpass_textView);
		edtPhone.setOnEditorActionListener(new EditText.OnEditorActionListener() {
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_DONE) {
					getpassword();
				}
				return false;
			}
		});
		findViewById(R.id.forgetpass_button).setOnClickListener(clickListener);
	}

	private void getpassword() {
		if (!edtPhone.getText().toString().equals("")) {
			textCaution.setText("");
			new AsyncTaskParseJson().execute(edtPhone.getText().toString());
		} else
			textCaution.setText("Tài khoản không hợp lệ!");
	}

	OnClickListener clickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.forgetpass_button:
				getpassword();
				break;
			default:
				break;
			}
		}
	};

	public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
		ProgressDialog progressDialog = new ProgressDialog(AccountForgetPassActivity.this);

		@Override
		protected void onPreExecute() {
			if (!progressDialog.isShowing())
				progressDialog.show();
		}

		@Override
		protected String doInBackground(String... arg0) {
			String str = JsonParser.changePass(phone,oldpass,newpass,renewpass);
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
//			BaseEngine.showLogformLongString("", result);
			if (progressDialog.isShowing())
				progressDialog.hide();
			if (result != null)
				try {
					ArrayList<String> list = JsonParser.readStatus(result);
					if (list != null&& list.size()>0) {
//						if (list.get(0).equals(CMD.TAG_SUCCESS)) {
							textCaution.setText(list.get(1));
//						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
	}
}
